import axios from "axios";
import { MessageBox, Message } from "element-ui";
import store from "@/store";
import { getToken } from "@/utils/auth";
const instance = axios.create({
  baseURL: "/api",
  timeout: 10000,
});
// 添加请求拦截器
instance.interceptors.request.use(
  function (config) {
    // 在发送请求之前做些什么
    if (store.state.user.Token) {
      console.log(process.env);
      config.headers.Authorization = `Bearer ${store.state.user.Token}`;
    }
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// 添加响应拦截器
instance.interceptors.response.use(
  function (response) {
    // 对响应数据做点什么
    const { data, success, message } = response.data;
    if (success) {
      return data;
    } else {
      Message({ type: "error", message });
    }
    return Promise.reject(new Error(message));
  },
  function (error) {
    // 对响应错误做点什么
    Message({ type: "error", message: error.message });
    return Promise.reject(error);
  }
);

export default instance;
